package com.chuang.bootplus.controller.document;


import com.chuang.bootplus.base.utils.ApiResponse;
import com.chuang.bootplus.entity.document.DMapT;
import com.chuang.bootplus.entity.document.DMapW;
import com.chuang.bootplus.entity.document.Document;
import com.chuang.bootplus.service.DMapTService;
import com.chuang.bootplus.service.DMapWService;
import com.chuang.bootplus.service.DocumentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chang
 * @since 2022-06-21
 */
@Api(tags = {"Document"})
@RestController
@RequestMapping("/document")
public class DocumentController {

    @Autowired
    DMapTService dMapTService;

    @Autowired
    DMapWService dMapWService;

    @Autowired
    DocumentService documentService;


//
//    @Transactional
//    @PostMapping("/addAll2")
//    @ApiOperation("load  Document 文件 ")
//    public ApiResponse<Void> addFile(@RequestBody TopicPO[] po) throws IOException {
//        String jsonStr = "";
//        try {
//            File jsonFile = new File("C:\\Users\\jokehe\\Desktop\\documents.json");
//            FileReader fileReader = new FileReader(jsonFile);
//            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
//            int ch = 0;
//            StringBuffer sb = new StringBuffer();
//            while ((ch = reader.read()) != -1) {
//                sb.append((char) ch);
//            }
//            fileReader.close();
//            reader.close();
//            jsonStr = sb.toString();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        List<DocumentPO> documentPOS = JSONObject.parseArray(jsonStr, DocumentPO.class);
//        System.out.println(documentPOS.size());
//
//
//        List<Document> list=new LinkedList<>();
//        List<DMapW> list1=new LinkedList<>();
//        List<DMapT> list2=new LinkedList<>();
//
//        for (DocumentPO d:documentPOS){
////            System.out.println(d.getWord_dis().length);
//            list.add(new Document(d.getId(),d.getName(),d.getContent(),d.getDate(),d.getTitle(),null,null));
//            for (TopicDisModel td:d.getTopic_dis()){
//                list2.add(new DMapT().setTId(td.getT_id()).setValue(td.getValue()).setTName(td.getT_name()).setDId(d.getId()));
//            }
//            for (WordDisModel wd:d.getWord_dis()){
//                list1.add(new DMapW().setWId(wd.getWord_id()).setValue(wd.getValue()).setDId(d.getId()));
//            }
//        }
//
//
//        dMapTService.saveBatch(list2);
//        dMapWService.saveBatch(list1);
//        documentService.saveBatch(list);
//
//        System.out.println("导入document 完成");
//        return new ApiResponse<>();
//    }
//
//

    @Transactional
    @PostMapping("/getDocument")
    @ApiOperation("通过documentid ")
    public ApiResponse<Document> addFile(@RequestParam String documentId)  {
        Document document = documentService.getById(documentId);
        HashMap<String,Object> columnMap = new HashMap<>();
        columnMap.put("d_id",documentId);
        List<DMapW> dmws = dMapWService.listByMap(columnMap);
        List<DMapT>  dmts = dMapTService.listByMap(columnMap);


        HashMap<String,Object>  resultMap = new HashMap<>();
        resultMap.put("document_base_info",document);
        resultMap.put("dmws",dmws);
        resultMap.put("dmts",dmts);


        ApiResponse repose = new ApiResponse<>();
        repose.setBody(resultMap);

        return repose;
    }



}

